import {
  initRightExample,
  initRightDocument,
  initRightBackground,
  renderDocumentCanvas,
  renderExampleCanvas,
  renderBackgroundCanvas
} from './dom'
import { activeMenu, activeTab } from './layout'

// 路由监听
window.addEventListener('hashchange', () => updateRouter(location.hash))

export function updateRouter(hash: string) {
  if (hash == '' || hash == '#/' || hash == '#/example') {
    location.hash = '/example/1.scene'
    return
  }
  if (hash == '#/background') {
    location.hash = '/background/background01'
    return
  }
  if (hash == '#/document') {
    location.hash = '/document/1.overview'
    return
  }
  if (hash.startsWith('#/example')) {
    activeTab('example')
    activeMenu(hash.replace('#/example/', ''))
    let { iframe } = initRightExample()
    let file = location.hash.replace('#/example/', '')
    renderExampleCanvas(file, iframe)
  }
  if (hash.startsWith('#/background')) {
    activeTab('background')
    activeMenu(hash.replace('#/background/', ''))
    let iframe = initRightBackground()
    let file = location.hash.replace('#/background/', '')
    renderBackgroundCanvas(file, iframe)
  }
  if (hash.startsWith('#/document')) {
    activeTab('document')
    activeMenu(hash.replace('#/document/', ''))
    let { iframe } = initRightDocument()
    let file = location.hash.replace('#/document/', '')
    renderDocumentCanvas(file, iframe)
  }
}
